package com.czh.lg43homework.controller;

import com.czh.lg43homework.model.LgResumeDatas;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/add")
    public String add(Integer key) {
        MongoOperations mongoOps = new MongoTemplate(MongoClients.create(
                "mongodb://192.168.31.250:27014"), "lg_resume");
        LgResumeDatas lgResumeDatas = new LgResumeDatas();
        lgResumeDatas.setShardKey(key);
        mongoOps.insert(lgResumeDatas, "lg_resume_datas");

        return "success";
    }

    @GetMapping("/list")
    public List<LgResumeDatas> list(Integer key) {
        MongoOperations mongoOps = new MongoTemplate(MongoClients.create(
                "mongodb://192.168.31.250:27014"), "lg_resume");

        return mongoOps.find(new Query(), LgResumeDatas.class, "lg_resume_datas");
    }
}
